package rlog

import (
	"fmt"
	"github.com/mattn/go-colorable"
	"path"
	"runtime"
	"strings"
	"time"
)

func init() {
	std.SetRuntimeCaller(3)
}

var std = NewLogger(colorable.NewColorableStdout())

func Print(a ...any) {
	std.Print(a...)
}

func Println(a ...any) {
	std.Println(a...)
}

func Printf(format string, a ...any) {
	std.Printf(format, a...)
}

func Debug(a ...any) {
	std.Debug(a...)
}

func DebugF(format string, a ...any) {
	std.DebugF(format, a...)
}

func Info(a ...any) {
	std.Info(a...)
}

func InfoF(format string, a ...any) {
	std.InfoF(format, a...)
}

func Success(a ...any) {
	std.Success(a...)
}

func SuccessF(format string, a ...any) {
	std.SuccessF(format, a...)
}

func Warn(a ...any) {
	std.Warn(a...)
}

func WarnF(format string, a ...any) {
	std.WarnF(format, a...)
}

func Error(a ...any) {
	std.Error(a...)
}

func ErrorF(format string, a ...any) {
	std.ErrorF(format, a...)
}

func Panic(a ...any) {
	std.Panic(a...)
}

func PanicF(format string, a ...any) {
	std.PanicF(format, a...)
}

func ExitError(a ...any) {
	std.ExitError(a...)
}

func ExitErrorAndSleep3(a ...any) {
	std.ExitErrorAndSleep3(a...)
}

// PrintlnConsoleMust 强制打印到控制台日志
func PrintlnConsoleMust(a ...any) {
	std.PrintlnConsoleMust(a...)
}

// PrintlnFileMust 强制打印到文件
func PrintlnFileMust(a ...any) {
	std.PrintlnFileMust(a...)
}

// PrintlnConsoleMustOnly 强制只打印到控制台日志
func PrintlnConsoleMustOnly(a ...any) {
	std.PrintlnConsoleMustOnly(a...)
}

// PrintlnFileMustOnly 强制只打印到文件
func PrintlnFileMustOnly(a ...any) {
	std.PrintlnFileMustOnly(a...)
}

// SetLogFilePath 设置日志文件路径
func SetLogFilePath(filepath string, oldLogToZip bool) {
	std.SetLogFilePath(filepath, oldLogToZip)
}

// SetConsoleLevel 设置控制台日志打印等级
func SetConsoleLevel(level Level) {
	std.SetConsoleLevel(level)
}

// SetFileLevel 设置文件日志打印等级
func SetFileLevel(level Level) {
	std.SetFileLevel(level)
}

// SetFullTextStaining 设置是否全文着色，默认false：tag 着色。
func SetFullTextStaining(full bool) {
	std.SetFullTextStaining(full)
}

// SetAutoDeleteOldLogFile 设置自动删除多少天前的日志
func SetAutoDeleteOldLogFile(day uint) {
	std.SetAutoDeleteOldLogFile(day)
}

// SetLogFileMaxSize 设置日志文件最大大小，MB
func SetLogFileMaxSize(mb uint) {
	std.SetLogFileMaxSize(mb)
}

// Recover 崩溃恢复，并且打印崩溃日志。
func Recover() {
	if _err := recover(); _err != nil {
		var buf [KB * 200]byte
		n := runtime.Stack(buf[:], false)
		_, file, line, _ := runtime.Caller(4)
		if line <= 0 {
			_, file, line, _ = runtime.Caller(2)
		} else {
			if strings.Contains(file, "/asm_amd") {
				_, file, line, _ = runtime.Caller(3)
			}
		}

		now := time.Now()
		year, month, day := now.Date()
		hour, minute, sec := now.Clock()
		mill := now.UnixMilli() % 1000
		var str strings.Builder
		str.WriteString(tagPanic)
		if projectDirLength == 0 || projectDirLength > len(file) {
			str.WriteString(fmt.Sprintf("%4d/%02d/%02d·%02d:%02d:%02d.%03d %v:%v ：", year, int(month), day, hour, minute, sec, mill, path.Base(file), line))
		} else {
			str.WriteString(fmt.Sprintf("%4d/%02d/%02d·%02d:%02d:%02d.%03d %v:%v ：", year, int(month), day, hour, minute, sec, mill, file[projectDirLength:], line))
		}
		str.WriteString(fmt.Sprintln(_err))
		str.Write(buf[:n])
		str.WriteString("\r\n")
		std.recover(str.String())
	}
}
